package com.cskaoyan.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.mall.product.cache.RedisCache;
import com.cskaoyan.mall.product.converter.dto.CategoryConverter;
import com.cskaoyan.mall.product.converter.dto.TrademarkConverter;
import com.cskaoyan.mall.product.dto.*;
import com.cskaoyan.mall.product.mapper.*;
import com.cskaoyan.mall.product.model.*;
import com.cskaoyan.mall.product.query.CategoryTrademarkParam;
import com.cskaoyan.mall.product.service.CategoryService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    FirstLevelCategoryMapper firstLevelCategoryMapper;
    @Autowired
    CategoryConverter categoryConverter;
    @Autowired
    TrademarkConverter trademarkConverter;
    @Autowired
    SecondLevelCategoryMapper secondLevelCategoryMapper;
    @Autowired
    ThirdLevelCategoryMapper thirdLevelCategoryMapper;
    @Autowired
    CategoryTrademarkMapper categoryTrademarkMapper;
    @Autowired
    TrademarkMapper trademarkMapper;
    @Autowired
    CategoryHierarchyMapper categoryHierarchyMapper;
    @Autowired
    CategoryServiceMapper categoryServiceMapper;
    @Override
    public List<FirstLevelCategoryDTO> getFirstLevelCategory() {
        //获取一级列表的全部数据，要用myBatisPlus，这里要调用，，mapper？FirstLevel的mapper？
        List<FirstLevelCategory> firstLevelCategories = firstLevelCategoryMapper.selectList(null);
        List<FirstLevelCategoryDTO> firstLevelCategoryDTOS = categoryConverter.firstLevelCategoryPOs2DTOs(firstLevelCategories);
        return firstLevelCategoryDTOS;
    }

    @Override
    public List<SecondLevelCategoryDTO> getSecondLevelCategory(Long firstLevelCategoryId) {
        //获取第二级的属性，根据第一级的属性
        //这里应该是要用一个wapper,用map即可
        Map<String, Object> map=new HashMap<>();
        map.put("first_level_category_id",firstLevelCategoryId);
        List<SecondLevelCategory> secondLevelCategories = secondLevelCategoryMapper.selectByMap(map);
        List<SecondLevelCategoryDTO> secondLevelCategoryDTOS = categoryConverter.secondLevelCategoryPOs2DTOs(secondLevelCategories);
        return secondLevelCategoryDTOS;
    }

    @Override
    public List<ThirdLevelCategoryDTO> getThirdLevelCategory(Long secondLevelCategoryId) {
        Map<String, Object> map=new HashMap<>();
        map.put("second_level_category_id",secondLevelCategoryId);
        List<ThirdLevelCategory> thirdLevelCategories = thirdLevelCategoryMapper.selectByMap(map);
        List<ThirdLevelCategoryDTO> thirdLevelCategoryDTOS = categoryConverter.thirdLevelCategoryPOs2DTOs(thirdLevelCategories);
        return thirdLevelCategoryDTOS;
    }

    @Override
    public List<TrademarkDTO> findTrademarkList(Long category3Id) {
        Map<String,Object> findCategoryTrademarkMap=new HashMap<>();
        findCategoryTrademarkMap.put("third_level_category_id",category3Id);
        List<CategoryTrademark> categoryTrademarkList = categoryTrademarkMapper.selectByMap(findCategoryTrademarkMap);
        if (categoryTrademarkList.isEmpty()){
            return new ArrayList<>();
        }
        List<Long> trademarkIdList=categoryTrademarkList.stream()
                .map(item->item.getTrademarkId())
                .collect(Collectors.toList());

        QueryWrapper<Trademark> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("id",trademarkIdList);

        List<Trademark> trademarks = trademarkMapper.selectList(queryWrapper);

        List<TrademarkDTO> trademarkDTOList = trademarkConverter.trademarkPOs2DTOs(trademarks);

        return trademarkDTOList;
    }

    @Override
    public void save(CategoryTrademarkParam categoryTrademarkParam) {
        //循环品牌id创建连接
        Date current=new Date();
        for (Long trademarkId : categoryTrademarkParam.getTrademarkIdList()) {
            CategoryTrademark categoryTrademark = new CategoryTrademark();
            categoryTrademark.setThirdLevelCategoryId(categoryTrademarkParam.getCategory3Id());
            categoryTrademark.setTrademarkId(trademarkId);
            categoryTrademark.setCreateTime(current);
            categoryTrademark.setUpdateTime(current);
            categoryTrademark.setIsDeleted(0);
            categoryTrademarkMapper.insert(categoryTrademark);
        }

    }

    @Override
    public List<TrademarkDTO> findUnLinkedTrademarkList(Long thirdLevelCategoryId) {
        Map<String,Object> findCategoryTrademarkMap=new HashMap<>();
        findCategoryTrademarkMap.put("third_level_category_id",thirdLevelCategoryId);
        List<CategoryTrademark> categoryTrademarkList = categoryTrademarkMapper.selectByMap(findCategoryTrademarkMap);
        List<Long> trademarkIdList=categoryTrademarkList.stream()
                .map(item->item.getTrademarkId())
                .collect(Collectors.toList());

        QueryWrapper<Trademark> queryWrapper=new QueryWrapper<>();
        if (!trademarkIdList.isEmpty()){
            queryWrapper.notIn("id",trademarkIdList);
        }

        List<Trademark> trademarks = trademarkMapper.selectList(queryWrapper);

        List<TrademarkDTO> trademarkDTOList = trademarkConverter.trademarkPOs2DTOs(trademarks);

        return trademarkDTOList;
    }

    @Override
    public void remove(Long thirdLevelCategoryId, Long trademarkId) {
        Map<String,Object> map=new HashMap<>();
        map.put("third_level_category_id",thirdLevelCategoryId);
        map.put("trademark_id",trademarkId);
        categoryTrademarkMapper.deleteByMap(map);

    }

    @Override
    @RedisCache(prefix = "product:getCategoryViewByCategoryId")
    public CategoryHierarchyDTO getCategoryViewByCategoryId(Long thirdLevelCategoryId) {
        List<CategoryHierarchy> categoryHierarchies = categoryHierarchyMapper.selectCategoryHierarchy(thirdLevelCategoryId);
        if (!categoryHierarchies.isEmpty()){
            return categoryConverter.categoryViewPO2DTO(categoryHierarchies.get(0));
        }
        return null;
    }

    @Override
    @RedisCache(prefix = "product:getCategoryTreeList")
    public List<FirstLevelCategoryNodeDTO> getCategoryTreeList() {
        //这里要写一个缓存，封装成一个注解
        //先把业务写了把，写连表查
        List<FirstLevelCategoryNodeDTO> categoryTreeList = categoryServiceMapper.getCategoryTreeList();
        return categoryTreeList;
    }
}
